<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <img src="./binggo.png" alt="">
    <title>生命周期</title>
</head>

<body>
    <div id="root">
        <h1 :style="{opacity:opacity}">欢迎学习VUE</h1>
        <button @click="opacity=1">透明度设置为1</button>
        <button @click="stop">点击停止</button>
    </div>
</body>
<script>
    const vm = new Vue({
        el: "#root",
        data: {
            opacity: 1,
        },
        methods: {
            stop() {
                // 只停止定时器
                // clearInterval(this.id)
                // vm消除 定时器未停止
                this.$destroy()
            }


        },

        mounted() {
            this.id = setInterval(() => {
                this.opacity -= 0.01
                if (this.opacity <= 0) this.opacity = 1
            }, 16)
        },
        // 关掉所有的定时器 消息订阅等
        beforeDestroy() {
            clearInterval(this.id)
        },

    })


</script>

</html>